﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WilsonWebsite
{
    public partial class Sku : System.Web.UI.Page
    {
        private int catId = 0;
        private string strCon = Global.strCon;
        private int proListNum = Global.seriesNum;
        private int recommNum = Global.recommNum;
        private int commctrlNum = 0;
        protected string imageWeb = Global.imageWeb;
        protected int catAdNum = Global.catAdNum;
        protected Dictionary<string, string> dic = new Dictionary<string, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                catId = Convert.ToInt32(Request["catId"]);
                BindCat(strCon, catId);
                BindCommCtrlItem();
                BindCatAd(strCon, catId, 2);
                BindSeries(strCon, catId);
                CatInfo(Global.strCon, catId);
            }
        }

        #region catId对应分类广告
        private void BindCat(string strCon,int catId)
        {
            using (DataTable dtCatAd = Bll.CategoryAd.MULTIPLE(strCon, catId, catAdNum)) 
            {
                if (dtCatAd != null && dtCatAd.Rows.Count > 0)
                {
                    this.repCommList.DataSource = dtCatAd.DefaultView;
                    this.repCommList.DataBind();
                    commctrlNum = dtCatAd.Rows.Count;
                }
            }
        }

        private DataTable CreateCommCtrlTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(Int32));
            dt.Columns.Add("Item", typeof(string));
            return dt;
        }

        private void BindCommCtrlItem()
        {
            DataTable dtcc = CreateCommCtrlTable();
            for (int i = 0; i < commctrlNum; i++)
            {
                DataRow newRow = dtcc.NewRow();
                newRow["Id"] = i + 1;
                newRow["Item"] = "<a href=\"javascript:void(0);\" class=\"CommCtrlItem\"><span></span></a>";
                dtcc.Rows.Add(newRow);
            }
            if (dtcc != null && dtcc.Rows.Count > 0)
            {
                this.repCommCtrl.DataSource = dtcc.DefaultView;
                this.repCommCtrl.DataBind();
                dtcc.Dispose();
            }
        }
        #endregion

        #region catId对应的广告
        private void BindCatAd(string strCon,int catId,int num) 
        {
            using (DataTable dtCAd = Bll.CategoryAd.MULTIPLE(strCon,catId,num)) 
            {
                if (dtCAd != null && dtCAd.Rows.Count > 0) 
                {
                    this.repCatAdList.DataSource = dtCAd.DefaultView;
                    this.repCatAdList.DataBind();
                    dtCAd.Dispose();
                }
            }
        }
        #endregion

        #region catId下的系列
        protected void BindSeries(string strCon, int catId)
        {
            using (DataTable dtp = Bll.Series.MULTIPLE(strCon,catId))
            {
                if (dtp != null && dtp.Rows.Count > 0)
                {
                    var query = from q in dtp.AsEnumerable()
                                where Convert.ToInt32(q["RowId"]) <= proListNum
                                select q;
                    this.repSeries.DataSource = query.CopyToDataTable<DataRow>().DefaultView;
                    this.repSeries.DataBind();
                }
            }
        }
        #endregion

        #region 分类信息
        protected void CatInfo(string strCon,int catId) 
        {
            using(DataTable dtCatInfo = Bll.Category.Single(strCon,catId))
            {
                if (dtCatInfo != null && dtCatInfo.Rows.Count > 0) 
                {
                    dic["Title"] = "Wilson - " + dtCatInfo.Rows[0]["CatName"].ToString();
                    dic["Keywords"] = dtCatInfo.Rows[0]["CatName"].ToString() + "，Wilson中国，AmerSports";
                    dic["Discription"] = dtCatInfo.Rows[0]["Description"].ToString();
                }
            }
        }
        #endregion
    }
}
